<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="公司" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          placeholder="请选择公司"
          filterable
          clearable
        >
          <el-option
            v-for="option in companyList"
            :key="option.id"
            :label="option.companyName"
            :value="option.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模型来源" prop="elementFrom">
        <el-select
          v-model="queryParams.elementFrom"
          placeholder="请选择模型来源"
          clearable
        >
          <el-option :key="1" label="平台" :value="1"></el-option>
          <el-option :key="2" label="供应商" :value="2"></el-option>
          <el-option :key="3" label="算力用户" :value="3"></el-option>
          <el-option :key="4" label="中国算力网" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模型共享类型" prop="elementShareType">
        <el-select
          v-model="queryParams.elementShareType"
          placeholder="请选择模型共享类型"
          clearable
        >
          <el-option :key="0" label="私密" :value="0"></el-option>
          <el-option :key="1" label="共享" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option :key="-2" label="驳回" :value="-2"></el-option>
          <el-option :key="-1" label="待审核" :value="-1"></el-option>
          <el-option :key="0" label="下架" :value="0"></el-option>
          <el-option :key="1" label="正常" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否可下载" prop="downloadType">
        <el-select
          v-model="queryParams.downloadType"
          placeholder="请选择是否可下载"
          clearable
        >
          <el-option :key="-1" label="不可以" :value="-1"></el-option>
          <el-option :key="0" label="可以" :value="0"></el-option>
          <el-option :key="1" label="线下协商" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模型框架" prop="elementFrame">
        <el-select
          v-model="queryParams.elementFrame"
          placeholder="请选择模型框架"
          clearable
        >
          <el-option
            v-for="dict in dict.type.element_frame"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="方向、领域" prop="elementDomain">
        <el-select
          v-model="queryParams.elementDomain"
          placeholder="请选择方向、领域"
          clearable
        >
          <el-option
            v-for="dict in dict.type.element_domain"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="elementTag">
        <el-select
          v-model="queryParams.elementTag"
          placeholder="请选择标签"
          clearable
        >
          <el-option
            v-for="dict in dict.type.element_tag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="平台推荐" prop="recommendStatus">
        <el-select
          v-model="queryParams.recommendStatus"
          placeholder="请选择平台推荐"
          clearable
        >
          <el-option :key="0" label="否" :value="0"></el-option>
          <el-option :key="1" label="是" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['element:model:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="elementProvideList"
      @selection-change="handleSelectionChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center" /> <el-table-column label="序号" prop="orderNum" width="70" align="center" />-->
      <el-table-column label="ID" align="center" prop="id" width="50" />
      <el-table-column
        label="模型名称"
        align="center"
        prop="elementTitle"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="模型来源"
        align="center"
        prop="elementFrom"
        width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.elementFrom === 1">平台</span>
          <span v-if="scope.row.elementFrom === 2">供应商</span>
          <span v-if="scope.row.elementFrom === 3">算力用户</span>
          <span v-if="scope.row.elementFrom === 4">中国算力网</span>
          <span v-if="scope.row.elementFrom === 5">企业用户</span>
        </template>
      </el-table-column>

      <el-table-column label="共享类型" align="center" prop="elementShareType">
        <template slot-scope="scope">
          <span v-if="scope.row.elementShareType === 0">私密</span>
          <span v-if="scope.row.elementShareType === 1">共享</span>
        </template>
      </el-table-column>

      <el-table-column
        label="是否可下载"
        width="120"
        align="center"
        prop="downloadType"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.downloadType === -1">不可以</span>
          <span v-if="scope.row.downloadType === 0">可以</span>
          <span v-if="scope.row.downloadType === 1">线下协商</span>
        </template>
      </el-table-column>
      <el-table-column
        label="模型框架"
        width="100"
        align="center"
        prop="elementFrame"
      >
        <template #default="{ row }">
          <dict-tag
            :value="row.elementFrame"
            :options="dict.type.element_frame"
          />
        </template>
      </el-table-column>
      <el-table-column label="下载次数" align="center" prop="downloadTimes" />
      <el-table-column label="收藏次数" align="center" prop="collectTimes" />
      <el-table-column label="平台推荐" align="center" prop="recommendStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.recommendStatus === 0">否</span>
          <span v-if="scope.row.recommendStatus === 1">是</span>
        </template>
      </el-table-column>
      <el-table-column
        label="研究方向、研究领域"
        align="center"
        prop="elementDomainList"
        width="180"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <dict-tag
            v-for="tag in row.elementDomainList"
            :key="tag"
            :value="tag"
            :options="dict.type.element_domain"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="模型标签"
        align="center"
        prop="elementTagList"
        width="180"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <dict-tag
            v-for="tag in row.elementTagList"
            :key="tag"
            :value="tag"
            :options="dict.type.element_tag"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="节点名"
        align="center"
        prop="supplyNodeName"
        width="100"
      />
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建人公司"
        align="center"
        prop="createUser.companyName"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        width="100"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <span v-if="row.status === -1">未审核</span>
          <span v-else-if="row.status === -2">已驳回</span>
          <template v-else>
            <el-switch
              :value="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toggleStatus(row)"
            ></el-switch>
            <span>{{ row.status ? "上架" : "下架" }}</span>
          </template>
        </template>
      </el-table-column>
      <!-- <el-table-column label="描述" align="center" prop="remark" /> -->
      <el-table-column label="模型文件" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="text" @click="goFile(row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleToggleIsTop(scope.row)"
            >{{ scope.row.recommendStatus ? "取消置顶" : "置顶" }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['\t\n'+
'element:model:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['element:model:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型信息-模型、数据集对话框 -->
    <el-dialog :title="title"    :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="模型名称" prop="elementTitle">
          <el-input
            v-model="form.elementTitle"
            placeholder="请输入"
            :maxlength="20"
          ></el-input>
        </el-form-item>

        <el-form-item label="模型共享类型" prop="elementShareType">
          <el-select
            v-model="form.elementShareType"
            placeholder="请选择模型共享类型"
          >
            <el-option :key="0" label="私密" :value="0"></el-option>
            <el-option :key="1" label="共享" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可下载" prop="downloadType">
          <el-select v-model="form.downloadType" placeholder="请选择是否可下载">
            <el-option :key="-1" label="不可以" :value="-1"></el-option>
            <el-option :key="0" label="可以" :value="0"></el-option>
            <el-option :key="1" label="线下协商" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模型框架" prop="elementFrame">
          <el-select v-model="form.elementFrame" placeholder="请选择模型框架">
            <el-option
              v-for="dict in dict.type.element_frame"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="平台推荐" prop="recommendStatus">
          <el-select v-model="form.recommendStatus" placeholder="请选择">
            <el-option :key="0" label="否" :value="0"></el-option>
            <el-option :key="1" label="是" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="研究方向、研究领域" prop="elementDomainList">
          <el-checkbox-group v-model="form.elementDomainList">
            <el-checkbox
              v-for="item in dict.type.element_domain"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="模型标签" prop="elementTagList">
          <el-checkbox-group v-model="form.elementTagList">
            <el-checkbox
              v-for="item in dict.type.element_tag"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="描述">
          <editor v-model="form.remark" :min-height="252" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listElementProvide,
  getElementProvide,
  delElementProvide,
  addElementProvide,
  updateElementProvide,
  toggleStatus,
  toggleProvideIsTop,
} from "@/api/element/elementProvide";
import companyMixin from "@/mixins/company";

export default {
  name: "ElementProvide",
  dicts: ["element_domain", "element_frame", "element_tag"],
  mixins: [companyMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型信息-模型、数据集表格数据
      elementProvideList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        elementFrom: null,
        elementShareType: null,
        status: null,
        downloadType: null,
        elementFrame: null,
        recommendStatus: null,
        elementDomain: null,
        elementTag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getCompanyList();
    this.getList();
  },
  methods: {
    async toggleStatus(row) {
      const { id, status } = row;
      const { code } = await toggleStatus({
        id,
        status: Number(!status),
      });
      if (code === 200) {
        this.$message.success(status ? "下架成功" : "上架成功");
        this.$set(row, "status", Number(!status));
      }
    },
    goFile(row) {
      if (![0, 1].includes(row.status))
        return this.$message.warning("审核未通过");
      localStorage.setItem("elementProvideCreateUserId", row.createUserId);
      this.$router.push(`/element/elementProvide/file?id=${row.id}`);
    },
    /** 查询模型信息-模型、数据集列表 */
    getList() {
      this.loading = true;
      listElementProvide({
        ...this.queryParams,
        elementType: 2,
      }).then((response) => {
        this.elementProvideList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        remark: null,
        elementFrom: null,
        elementType: 2,
        elementShareType: null,
        downloadTime: null,
        status: null,
        downloadType: null,
        elementFrame: null,
        downloadTimes: null,
        collectTimes: null,
        recommendStatus: null,
        elementDomainList: [],
        elementTagList: [],
        updateUserId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getElementProvide(id).then((response) => {
        this.form = response.data;
        // this.form.elementDomain = this.form.elementDomain.split(",");
        // this.form.elementTag = this.form.elementTag.split(",");
        this.open = true;
        this.title = "修改模型信息";
      });
    },
    async handleToggleIsTop(row) {
      await this.$confirm(
        `确定${row.recommendStatus ? "取消置顶" : "置顶"}该数据吗？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      );
      await toggleProvideIsTop({
        id: row.id,
        status: Number(!row.recommendStatus),
      });
      this.$message.success(`${row.recommendStatus ? "取消置顶" : "置顶"}成功`);
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.elementDomain = this.form.elementDomain.join(",");
          // this.form.elementTag = this.form.elementTag.join(",");
          if (this.form.id != null) {
            updateElementProvide(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElementProvide(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除模型信息-模型、数据集编号为"' + ids + '"的数据项？'
        )
        .then(function () {
          return delElementProvide(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "element/elementProvide/export",
        {
          ...this.queryParams,
        },
        `elementProvide_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
